/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.semi.fw;

import com.semi.util.StringUtil;
import com.semi.util.FileLogger;
import java.io.IOException;

/**
 *
 * @author liaoyq
 */
public class FwUser {

    private static final byte clscode[] = {0x1B, 0x5B, 0x48, 0x1B, 0x5B, 0x4A};    
    private final FileLogger mLogger = new FileLogger(FwUser.class.getName());
    protected SocketWrapper mChennel;
    private long mLastTime;
    private long mTimeout;    
    private StringBuilder message = new StringBuilder();
    protected int mInfo;
    protected FwCmd mExecute;
    private CodeCenter mRoot ;
    protected boolean mExit;
    protected int mSection;
    protected int mDir;
    public FwUser(SocketWrapper sc,CodeCenter root){
        mChennel = sc;
        mRoot = root;
        mTimeout = root.ConsoleTimeout();
        mLastTime = System.currentTimeMillis();
        mExecute = root.mExecute;       
        mExit =false;
        mDir = 0;
    }
    protected String getProp(){
        String tmp = mRoot.name();
        if( (mSection&0x0001)==1 ) tmp +="#";
        else tmp +=">";
        return tmp;
    }    
    public void SetExit(){ mExit = true; }
    public boolean IsExit(){ return mExit; }
    public boolean isTimeout(){
        if( mTimeout==0 ) return false;
        return System.currentTimeMillis() - this.mLastTime > this.mTimeout;
    }

    public void onConnected() throws IOException {
        mLogger.info("client "+ mChennel.remoteAddr()+" up");
        mChennel.write(FwCmd.MSG+getProp());
        mInfo = 1;       
        mSection =0;
    }

    public void onData(String  data) throws IOException{
        mLastTime = System.currentTimeMillis();
        if( data== null ) return;
        int k  = -1;
        if( data.charAt(0)>235 ){
            //
            return;
        }
        for( int i=0;i<data.length();i++){
            char ch= data.charAt(i);
            if( ch=='\r'||ch=='\n' ) {
                k=i;
                break;
            }
        }
        if(k<0){
            message.append(data);
            return;
        }        
        String tmp = data.substring(0,k);
        if( tmp!=null ) message.append(tmp);
        tmp = message.toString();
        if( (tmp!=null) && (tmp.length()>0) ){
            mLogger.debug("exec:"+tmp);
            try{
              tmp = mExecute.execute(tmp,this);
            }catch(Exception e){
               tmp = e.getMessage();
            }
            mLogger.debug("rep:"+tmp);
        } else tmp ="";
         tmp +=StringUtil.CRLF+getProp();
         message.delete(0, message.length());
        mLastTime = System.currentTimeMillis(); 
        mChennel.write(tmp);        
    }
    public void onClose(){
        Throwable e = new Throwable();
        mLogger.info("client "+ mChennel.remoteAddr()+" down",e);
    }
    public void onTimeOut() throws IOException{
        mChennel.write(FwCmd.MSG+StringUtil.CRLF+getProp());
    }
    public void cls() throws IOException{
        mChennel.write(clscode);
    }
}
